package cn.cmkj.auction.data.http;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.cmkj.auction.app.Urls;

/**
 * Created by cunguoyao on 2017/9/24.
 */

public class AuctionOrder implements Serializable {

    private String auctionId;
    private String orderId;
    private String typeStr;//
    private BigDecimal amount;
    private String goodsName;
    private String goodsImage;
    private String time;//倒计时时间
    private String orderStatus;//succ已结束 timeout超时 ready待支付 cancel已取消 return已退款 progress已发货
    private boolean payStatus;
    private String payStatusStr;

    private Address address;//额外加的

    //订单列表解析
    public static AuctionOrder parseFromJson(JSONObject json){
        AuctionOrder order = new AuctionOrder();
        if(json != null) {
            order.setOrderId(json.optString("order_id"));
            order.setTypeStr(json.optString("type"));
            double shouldPay = json.optDouble("amount", 0);
            order.setAmount(BigDecimal.valueOf(shouldPay));
            order.setGoodsName(json.optString("name"));
            JSONArray jsonArray = json.optJSONArray("item");
            if(jsonArray != null && jsonArray.length() > 0) {
                JSONObject jsonObject = jsonArray.optJSONObject(0);
                order.setGoodsImage(Urls.web + "/" + jsonObject.optString("m_url"));
            }
            JSONObject jsonObject = json.optJSONObject("time");
            if(jsonObject != null) {

            }else {
                order.setTime("");
            }
            order.setOrderStatus(json.optString("status"));
            order.setPayStatus(json.optBoolean("payStatusType"));
            order.setPayStatusStr(json.optBoolean("pay_status") ? "已支付" : "未支付");
        }
        return order;
    }

    public static List<AuctionOrder> parseFromJsonArray(JSONArray array){
        List<AuctionOrder> list = new ArrayList<>();
        if(array != null && array.length() > 0){
            for(int i=0;i<array.length();i++){
                list.add(parseFromJson(array.optJSONObject(i)));
            }
        }
        return list;
    }

    //订单详情解析
    public static AuctionOrder parseFromJsonDetail(JSONObject jsonObject) {
        if(jsonObject != null) {
            AuctionOrder order = new AuctionOrder();
            Address address = Address.parseFromJson(jsonObject.optJSONObject("consignee"));
            order.setAddress(address);
            order.setOrderStatus(jsonObject.optString("orderStatus"));
            order.setPayStatus(jsonObject.optBoolean("payStatusType", false));
            order.setPayStatusStr(jsonObject.optString("payStatus"));
            order.setTime(jsonObject.optString("createtime"));
            order.setTypeStr(jsonObject.optString("type"));
            order.setOrderId(jsonObject.optString("order_id"));
            double amount = jsonObject.optDouble("total_amount", 0);
            order.setAmount(BigDecimal.valueOf(amount));
            JSONObject jsonGoodsList = jsonObject.optJSONObject("goodsinfo").optJSONObject("goods");
            Shuhua shuhua = null;
            //通过迭代器获取这段json当中所有的key值
            Iterator keys = jsonGoodsList.keys();
            //然后通过一个循环取出所有的key值
            while (keys.hasNext()){
                String key = String.valueOf(keys.next());
                //最后就可以通过刚刚得到的key值去解析后面的json了
                String value = jsonGoodsList.optString(key, "");
                JSONObject jsonGoods = null;
                try {
                    jsonGoods = new JSONObject(value);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //商品列表
                shuhua = Shuhua.parseFromJsonForOrder(jsonGoods);
                break;
            }
            order.setGoodsName(shuhua.getName());
            order.setGoodsImage(shuhua.getDefaultImage().l_url);
            return order;
        }
        return null;
    }

    //拍卖预下单返回解析
    public static AuctionOrder parseFromJsonPreOrder(JSONObject jsonObject) {
        if(jsonObject != null) {
            AuctionOrder order = new AuctionOrder();
            Address address = Address.parseFromJson(jsonObject.optJSONObject("member_info").optJSONObject("def_addr"));
            order.setAddress(address);
            order.setOrderStatus("ready");
            order.setPayStatus(jsonObject.optBoolean("payStatusType", false));
            order.setTime(jsonObject.optString("createtime"));
            order.setTypeStr(jsonObject.optString("type"));
            order.setOrderId(jsonObject.optString("order_id"));
            double amount = jsonObject.optDouble("subtotal_price", 0);
            order.setAmount(BigDecimal.valueOf(amount));
            JSONObject jsonAuction = jsonObject.optJSONObject("object").optJSONObject("goods");
            Auction auction = Auction.parseFromJsonPreOrder(jsonAuction);
            order.setGoodsName(auction.getName());
            order.setAuctionId(""+auction.getId());
            JSONArray images = jsonObject.optJSONArray("images");
            if(images != null && images.length() > 0) {
                JSONObject image = images.optJSONObject(0);
                order.setGoodsImage(Urls.web + "/" + image.optString("m_url"));
            }
            return order;
        }
        return null;
    }

    public String getAuctionId() {
        return auctionId;
    }

    public void setAuctionId(String auctionId) {
        this.auctionId = auctionId;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getTypeStr() {
        return typeStr;
    }

    public void setTypeStr(String typeStr) {
        this.typeStr = typeStr;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getGoodsImage() {
        return goodsImage;
    }

    public void setGoodsImage(String goodsImage) {
        this.goodsImage = goodsImage;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public boolean isPayStatus() {
        return payStatus;
    }

    public void setPayStatus(boolean payStatus) {
        this.payStatus = payStatus;
    }

    public String getOrderStatus() {
        return orderStatus;
    }

    public String getPayStatusStr() {
        return payStatusStr;
    }

    public void setPayStatusStr(String payStatusStr) {
        this.payStatusStr = payStatusStr;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}
